package me.ymfs.springboot.guice.configuration;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.List;

@Configuration
public class WebMVCConfiguration implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();//4

        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat,// 标准输出
                //SerializerFeature.WriteClassName,// 输出对象类型
                SerializerFeature.WriteMapNullValue,// 显示null
                SerializerFeature.WriteNullListAsEmpty,// list null 转 []
                //SerializerFeature.WriteNullNumberAsZero,// 数值 null 转 0
                SerializerFeature.WriteNullStringAsEmpty,// 字符串 null 转 ""
                SerializerFeature.WriteDateUseDateFormat,// 日期格式化 yyyy-MM-dd HH:mm:ss
                SerializerFeature.DisableCircularReferenceDetect,// list 递归死循环处理
                SerializerFeature.IgnoreNonFieldGetter// 忽略没有get, set方法的属性
        );
        converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON_UTF8));

        converter.setFastJsonConfig(fastJsonConfig);
        converters.add(converter);
    }

}
